<template>
  <div
    class="login-container w-image-fit w-flex-center"
    :style="bgImg">
    <el-form
      ref="loginForm"
      :model="loginForm"
      :rules="loginRules"
      class="login-form"
      auto-complete="on"
      label-position="left">
      <h3 class="title">
        用户登录
      </h3>
      <el-form-item prop="user">
        <span class="svg-container svg-container_login">
          <icon-user
            width="16px"
            height="16px"
            fill="#fff"/>
        </span>
        <el-input
          v-model.trim="loginForm.user"
          name="user"
          type="text"
          auto-complete="on"
          placeholder="请输入用户名" />
      </el-form-item>
      <el-form-item prop="pass">
        <span class="svg-container">
          <icon-password
            width="16px"
            height="16px"
            fill="#fff"/>
        </span>
        <el-input
          :type="pwdType"
          v-model.trim="loginForm.pass"
          name="pass"
          auto-complete="on"
          placeholder="请输入密码"
          @keyup.enter.native="handleLogin" />
        <span
          class="show-pwd"
          @click="showPwd">
          <icon-eye
            width="16px"
            height="16px"
            fill="#fff"/>
        </span>
      </el-form-item>
      <el-form-item>
        <el-button
          :loading="loading"
          type="primary"
          style="width:100%;"
          @click.native.prevent="handleLogin">
          登录
        </el-button>
      </el-form-item>
      <p class="home">
        <span
          class="w-float-right w-pointer"
          @click="backHomeClick">返回首页</span>
      </p>
    </el-form>
  </div>
</template>

<script>
import { isvalidUsername, isvalidUserPass } from '@/utils/validate'
import IconUser from '@/icons/user.svg'
import IconPassword from '@/icons/password.svg'
import IconEye from '@/icons/eye.svg'
export default {
  name: 'Login',
  inject: ['bgImage'],
  components: {
    IconUser,
    IconPassword,
    IconEye
  },
  data() {
    const validateUsername = (rule, value, callback) => {
      if (!isvalidUsername(value)) {
        callback(new Error('用户名必须是数字或字母（4-20字符）'))
      } else {
        callback()
      }
    }
    const validatePass = (rule, value, callback) => {
      if (!isvalidUserPass(value)) {
        callback(new Error('密码必须是数字或字母（6-20字符）'))
      } else {
        callback()
      }
    }
    return {
      loginForm: {
        user: '',
        pass: ''
      },
      loginRules: {
        user: [{ required: true, trigger: 'blur', validator: validateUsername }],
        pass: [{ required: true, trigger: 'blur', validator: validatePass }]
      },
      loading: false,
      pwdType: 'password'
    }
  },
  computed: {
    /**
     * 获取随机的背景图片
     */
    bgImg() {
      const num = Math.floor(Math.random() * 40)
      return this.bgImage(`https://cdn.wangyabin.top/wallpaper/${num}.jpg`)
    }
  },
  methods: {
    showPwd() {
      if (this.pwdType === 'password') {
        this.pwdType = 'text'
      } else {
        this.pwdType = 'password'
      }
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          this.$store.dispatch('LOGIN', this.loginForm).then(() => {
            this.$router.push({ name: 'Admin' })
          }).catch((err) => {
            if (err.code === 'admin') {
              this.$w_popview.msgError('只有管理员才能登陆')
            }
          }).finally(() => {
            this.loading = false
          })
        } else {
          return false
        }
      })
    },
    // 返回首页
    backHomeClick() {
      this.$router.push('/')
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss">
$bg:#2d3a4b;
$light_gray:#eee;

/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;
    input {
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
      background-color: $bg;
      &:-webkit-autofill {
        -webkit-box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: #fff !important;
      }
    }
  }
  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    border-radius: 5px;
    color: #454545;
  }
}

</style>

<style rel="stylesheet/scss" lang="scss" scoped>
$dark_gray:#889aa4;
$light_gray:#eee;
.login-container {
  position: fixed;
  height: 100%;
  width: 100%;
  .login-form {
    background-color: rgba(0, 0, 0, .8);
    width: 520px;
    padding: 35px 35px 15px 35px;
    border-radius: 5px;
  }
  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;
    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }
  .svg-container {
    padding: 0px 5px 0px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
    &_login {
      font-size: 20px;
    }
  }
  .title {
    font-size: 26px;
    font-weight: 400;
    color: $light_gray;
    margin: 0px auto 40px auto;
    text-align: center;
    font-weight: bold;
  }
  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
  .home {
    color: #aaa;
    font-size: 14px;
  }
}
</style>
